//
//  MCBasicVersionCompatibility.m
//  MCPublicModule
//
//  Created by Mirko on 16/7/4.
//  Copyright © 2016年 Mirko. All rights reserved.
//

#import "MCBasicVersionCompatibility.h"
#import "HDEasterEggsCenter.h"
#import "MCBasicAppEngine.h"

static NSString *const kIsFirstRunKey = @"MC_FirstRun_Key";
static NSString *const kCoverInstallationKey = @"MC_Coverinstallation_Key";
static NSString *const kBackupVersionInfo = @"MC_BackupVersion_key";

@interface MCBasicVersionCompatibility () <HDEasterEggsDataSource, HDEasterEggsDelegate>

@end

@implementation MCBasicVersionCompatibility

-(instancetype) init
{
    self = [super init];
    if (self) {
        [[HDEasterEggsCenter easterEggsCenter] registerObject:self];
        [self versionCompatibility];
    }
    return self;
}

-(void) setFirstRun:(BOOL)firstRun
{
    [[NSUserDefaults standardUserDefaults] setBool:firstRun forKey:kIsFirstRunKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(BOOL) firstRun
{
    NSValue *value = [[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstRunKey];
    if (value) {
        return NO;
    }
    return YES;
}

-(void) cleanFirstRun
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kIsFirstRunKey];
    [self revertVersionCompatibility];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void) revertVersionCompatibility
{
    NSString *backupVersion = [[NSUserDefaults standardUserDefaults] valueForKey:kBackupVersionInfo];
    if (backupVersion) {
        [[NSUserDefaults standardUserDefaults] setValue:backupVersion forKey:kCoverInstallationKey];
    }
}

-(void) versionCompatibility
{
    NSString *versionCatch = [[NSUserDefaults standardUserDefaults] valueForKey:kCoverInstallationKey];
    if (!versionCatch) {
        [self recordVersionInfo];
        return;
    }
    
    if (versionCatch && [MCUtils compareVersion:versionCatch current:[MCUtils appReleaseVersion]] == NSOrderedAscending) {
        if ([self.delegate respondsToSelector:@selector(dataCompatibility)]) {
            [self.delegate dataCompatibility];
        }
        [self dataCompatibility];
        [self backupVersionInfo];
        [self recordVersionInfo];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

-(void) backupVersionInfo
{
    NSString *lastVersionStr = [[NSUserDefaults standardUserDefaults] valueForKey:kCoverInstallationKey];
    if (lastVersionStr) {
        [[NSUserDefaults standardUserDefaults] setValue:lastVersionStr forKey:kBackupVersionInfo];
    }
}

-(void) recordVersionInfo
{
    NSString *versionStr = [MCUtils appReleaseVersion];
    [[NSUserDefaults standardUserDefaults] setValue:versionStr forKey:kCoverInstallationKey];
}

-(void) dataCompatibility
{
    
}

#pragma mark HDEasterEggsDataSource HDEasterEggsDelegate
-(HDEETableCellModel *) cellInfo
{
    HDEETableCellModel *model = [HDEETableCellModel model];
    model.identifier = NSStringFromClass([MCBasicVersionCompatibility class]);
    model.cellTitle = @"Rever first run";
    model.type = kHDEasterEggsCellTypeForDefault;
    return model;
}


#pragma mark HDEasterEggsDelegate
-(void) handleCellAction
{
    DLOG(@"");
    [self cleanFirstRun];
    NSValue *value = [[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstRunKey];
    DLOG(@"First run value is %@", value);
    [MCBasicAppEngine showTostAlert:@"Rever complete!"];
}

-(void) handelCellSwicthAciton:(HDEETableCellModel *) cellModel
{
    DLOG(@"");
}


@end
